/*
 * @(#)BrandAction.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.action.brand;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hangame.mbl.action.SmartCommonBaseAction;
import com.hangame.mbl.bo.CommonBO;
import com.hangame.mbl.bo.brand.BrandBO;
import com.hangame.mbl.bo.brand.BrandBO.MidResult;
import com.hangame.mbl.constant.CommonConst;
import com.hangame.mbl.model.Brand;
import com.hangame.mbl.model.Common;
import com.nhncorp.lucy.web.metadata.Inject;

// TODO: Auto-generated Javadoc
/**
 * The Class BrandAction.
 * 
 * @author Lixing
 */
public class BrandAction extends SmartCommonBaseAction {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 2913841909459879330L;
	
	/** The log. */
	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(BrandAction.class);
	
	/** The brand bo. */
	private BrandBO brandBO;
	
	/** The common bo. */
	private CommonBO commonBO;
	
	/**
	 * Sets the brand bo.
	 * 
	 * @param brandBO the new brand bo
	 */
	@Inject(beanId = "brandBO")
	public void setBrandBO(BrandBO brandBO) {
		this.brandBO = brandBO;
	}
	
	/**
	 * Sets the common bo.
	 * 
	 * @param commonBO the new common bo
	 */
	@Inject(beanId = "commonBO")
	public void setCommonBO(CommonBO commonBO) {
		this.commonBO = commonBO;
	}
	
	/* (non-Javadoc)
	 * @see com.hangame.mbl.action.SmartPortalAction#execute()
	 */
	@Override
	public String execute() throws Exception {
		return brandList();
	}
	
	/**
	 * Brand list.
	 * 
	 * @return the string
	 * @throws Exception the exception
	 */
	public String brandList() throws Exception {
		
		String osType = params.getString("osType");
		
		List<Common> agencyList = commonBO.getCommonCodeAndName(CommonConst.COMMON_AGENCY);
		List<Brand> osList = brandBO.getCommentPhoneList(osType);
		
		setAttribute("agencyList", agencyList);
		setAttribute("osList", osList);
		setAttribute("gameNo", params.getString("gameNo"));
		setAttribute("osType", osType);
		
		return "popupExprcAply";
	}
	
	/**
	 * Send sms.
	 * 
	 * @return the string
	 * @throws Exception the exception
	 */
	public String sendSMS() throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		
		String phoneNo = params.getString("phoneNo");
		MidResult midRes = brandBO.sendPhoneOwnerSMS(phoneNo);
		if (midRes.hasError() == false) {
			result.put("result", "OK");
			result.put("seqId", midRes.getResult());
		} else {
			result.put("result", "ERROR");
			result.put("error", midRes.getError());
		}
		
		JSONObject object = JSONObject.fromObject(result);
		setAttribute("JSON_RESULT", object);
		return "commonJsonResult";
	}
	
	/**
	 * Write.
	 * 
	 * @return the string
	 * @throws Exception the exception
	 */
	public String write() throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		
		String gameNo = params.getString("gameNo");
		String phoneNum = params.getString("phoneNum");
		String gender = params.getString("gender");
		String age = params.getString("year");
		String agency = params.getString("agency");
		String udid = params.getString("udid");
		String osType = params.getString("osType");
		String cphMdlNm = params.getString("phoneType");
		
		String authNum = params.getString("authNum");
		String seqId = params.getString("seqId");
		int count = brandBO.checkedRegister(phoneNum);
		if (count > 0) {
			result.put("result", "ERROR");
			result.put("error", "이미 신청이 완료된 번호입니다.");
		} else if (count == -1) {
			result.put("result", "정상적으로 처리되지 않았습니다.");
		} else {
			MidResult midRes = brandBO.checkAuthNum(authNum, seqId);
			
			if (midRes.hasError() == false) {
				if ("IOS".equals(osType)) {
					udid = "";
				}
				int res = brandBO.writeRegister(gameNo, phoneNum, gender, age, osType, agency, udid, cphMdlNm);
				if (res != 1) {
					result.put("result", "정상적으로 처리되지 않았습니다.");
				} else {
					result.put("result", "OK");	
				}
			} else {
				result.put("result", "ERROR");
				result.put("error", midRes.getError());
			}
		}
		JSONObject object = JSONObject.fromObject(result);
		setAttribute("JSON_RESULT", object);
		return "commonJsonResult";
	}

	/**
	 * SMS 보내기 팝업 띄우기.
	 * 
	 * @return the string
	 * @throws Exception the exception
	 * @return
	 */
	public String popupSendSms() throws Exception {
		Map<String, List<Brand>> maplist = brandBO.getCommentPhoneList();
		setAttribute("androidList", maplist.get(CommonConst.OS_TYPE_CD_ANDROID));
		setAttribute("iosList", maplist.get(CommonConst.OS_TYPE_CD_IOS));
		setAttribute("agencyList", commonBO.getCommonCodeAndName(CommonConst.COMMON_AGENCY));
		return "popupAlarmSendSms";
	}
	
	/**
	 * save alarm information.
	 * 
	 * @return the string
	 * @throws Exception the exception
	 * @return
	 */
	public String createAlrmInfo() throws Exception {
		
		String cphNo = params.getString("phoneNum");
		String osTypeCd = params.getString("osType");
		String cphMdlNm = params.getString("phoneType");
		String cmncCoTypeCd = params.getString("agencyType");
		String authNum = params.getString("validateCode");
		String seqId = params.getString("seqId");
	
		Map<String, Object> result = new HashMap<String, Object>();
		int count = brandBO.checkAlarmByCphNo(cphNo);
		if (count > 0) {
			result.put("result", "ERROR");
			result.put("error", "이미 신청이 완료된 번호입니다.");
		} else if (count < 0) {
			result.put("result", "정상적으로 처리되지 않았습니다.");
		} else {
			MidResult midRes = brandBO.checkAuthNum(authNum, seqId);
			if (midRes.hasError() == false) {
				int res = brandBO.writeAlrmInfo(osTypeCd, cphMdlNm, cphNo, cmncCoTypeCd);
				if (res != 1) {
					result.put("result", "정상적으로 처리되지 않았습니다.");
				} else {
					result.put("result", "OK");	
				}
			} else {
				result.put("result", "ERROR");
				result.put("error", midRes.getError());
			}
		}
		
		JSONObject object = JSONObject.fromObject(result);
		setAttribute("JSON_RESULT", object);
		return "commonJsonResult";
	}
}
